package evil.wolf.panels;

import evil.wolf.action.Action;
import evil.wolf.business.BusinessTonDau;
import evil.wolf.excute.query.ExcuteQuery;
import evil.wolf.gui.MainUI;
import evil.wolf.gui.UITonDau;
import evil.wolf.objects.ObjectTonDau;



import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
//import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class ListTonDauUI extends JPanel implements MouseListener{
	
	// Static fields.
	MainUI mainUI; 

	int colCount;
	int index[];
	JPopupMenu menu = new JPopupMenu("Popup");
	ObjectTonDau objectTonDau = new ObjectTonDau();
	String action = null;
	
	public List<ObjectTonDau> arrayLaterials;
	public JTable dataTable;
	public NewTableModel dataModel;
	
	// Constructors.
	public ListTonDauUI(MainUI mainUI)	{
		super(new BorderLayout());
		this.mainUI = mainUI;
		
		dataModel = new NewTableModel();
		dataTable = new JTable(dataModel);
		dataTable.addMouseListener(this);
		
		dataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollpane = new JScrollPane(dataTable);
		add(scrollpane, BorderLayout.CENTER);
	    JMenuItem item = new JMenuItem("Edit selected row");
	  
	    item.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Menu item Test1Edit this row");
			}
	    });
	    
	    menu.add(item);
	    item = new JMenuItem("Delete selected row");
	    item.addActionListener(new ActionListener() {
	    	
	    	public void actionPerformed(ActionEvent e) {
	    	  
	    		if(JOptionPane.showConfirmDialog(null, "Do you really want to delete this row?", "CONFIRM", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
					int row = getSelectedRow();
					ExcuteQuery doAction = new ExcuteQuery();
					if(doAction.deleteTonDau((String)dataTable.getValueAt(getSelectedRow(), 0) , (String)dataTable.getValueAt(getSelectedRow(), 1))) {
						
						arrayLaterials.remove(row);
						dataModel.fireTableDataChanged();
					}
	    	 }
	      }
	    });
	    
	    menu.add(item);
	    dataModel.fireTableDataChanged();
	}
	

	public int getSelectedRow() {
		index  = dataTable.getSelectedRows();
		return index[0];
	}
	
	public String getAction(){
		return action;
	}
	
	public void setAction(String action){
		 this.action = action;
	}
	
	@Override
	public void mouseClicked(MouseEvent me) {
		
		if(me.getClickCount() == 2 && !me.isConsumed()) {
			me.consume();
			setAction(Action.ACTION_UPDATE);
			
			objectTonDau.setmah((String) dataTable.getValueAt( getSelectedRow(), 0));
			objectTonDau.setmak((String) dataTable.getValueAt( getSelectedRow(), 1));
			objectTonDau.setsl((int) dataTable.getValueAt( getSelectedRow(), 2));
			objectTonDau.setdg((int) dataTable.getValueAt(  getSelectedRow(), 3));
			objectTonDau.settg((int) dataTable.getValueAt(  getSelectedRow(), 4));
			UITonDau TonDauUI = new UITonDau(mainUI, this, objectTonDau);
			TonDauUI.setTextUI(objectTonDau);
			TonDauUI.setVisible(true);
		}
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent ev) {
		if (ev.isPopupTrigger() && this.getSelectedRow() != -1 ) {
	          menu.show(ev.getComponent(), ev.getX(), ev.getY());
	        }
		
	}
	@Override
	public void mouseReleased(MouseEvent ev) {
		if (ev.isPopupTrigger() && this.getSelectedRow() != -1 ) {
	          menu.show(ev.getComponent(), ev.getX(), ev.getY());
	    }
	}
	
public class NewTableModel extends AbstractTableModel {
	
		BusinessTonDau tondauBUS = new BusinessTonDau();
		
		// constructor 
		public NewTableModel() {
			arrayLaterials  = tondauBUS.getListRecords();
		}
		
		@Override
		public String getColumnName(int i) {
			
		    return tondauBUS.getName()[i];
		}
		
		@Override
		public int getColumnCount()	{
			return tondauBUS.getColumn();
		}
		
		@Override
		public int getRowCount() {
		
			return arrayLaterials.size();
		}
		
		@Override
		public Object getValueAt(int row, int col) {
			ObjectTonDau objectTonDau = (ObjectTonDau)arrayLaterials.get(row);
					
			switch (col) {
			case 0: return objectTonDau.getmah();
			case 1: return objectTonDau.getmak();
			case 2: return objectTonDau.getsl();
			case 3: return objectTonDau.getdg();
			case 4: return objectTonDau.gettg();
			default: return null;
			}
		}
	}
}